// seach.js
var iconUrl=require('../../config.js').iconUrl;
var blu = require('../../utils/blue.js');
const app = getApp();
Page({
  stop_search:function(){
    wx.navigateTo({
      url: '/pages/search/search?page=2',
    })
  },
  search_again:function(){
    wx.navigateTo({
      url: '/pages/search/search?page=3',
    })
  },
  blue_con:function(e){
    var deviceId = e.currentTarget.id;
    var val=e.detail.value;    
    if(val){//链接
      blu.blue_con(deviceId);    
    }else{//取消
       blu.blue_close();
    }    
    
    // wx.switchTab({
    //   url: '/pages/status/status',
    // })

  },
  /**
   * 页面的初始数据
   */
  data: {
    iconUrl: iconUrl,
     page:0,
     device_list:'',
     current_con_deviceId:'',
     selecteds:[],
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    var that=this;
    that.setData({
      page: options.page,
    });
    if(options.page==1){
      blu.blue_list(
        function (devices) { 
            console.log("devices",devices);           
            if(devices.length>0){
              wx.setStorageSync("device_list", devices);
              wx.navigateTo({
                url: '/pages/search/search?page=3',
              })
            }
        }
      );
    }    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {    
    var selecteds=[];     
    var devices=wx.getStorageSync("device_list");
    var current_diviceId=getApp().globalData.current_con_deviceId;
    
      devices.forEach(function(value){
            if(value.deviceId==current_diviceId){
              selecteds.push(true);
            }else{
              selecteds.push(false);
            }
      });
    this.setData({
      device_list:devices,
      current_con_deviceId:current_diviceId,
      selecteds: selecteds
    });      
    console.log("selecteds", selecteds);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("onHide-------");    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})